/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.izenman.Graphisme;

import android.app.Activity;
import android.content.Context;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.os.Bundle;
import android.view.*;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import se.izenman.Principal.RemoteAudioVideo;
import se.izenman.outilsServer.*;
 

/**
 *
 * @author romainmior
 */
public class MenuAndroid extends Activity  implements OnGestureListener, ITCPVLCFunctions, ITCPPTTFunctions{
 
    
    public static final int NO_REMOTE = 0;
    public static final int REMOTE_AUDIO = 1;
    public static final int REMOTE_PRESENTATION = 2;
    public static final int REMOTE_TRACKPAD = 3;
    
    //expression régulière adresse IPV4
    public static String regex = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
    
    public static int RemoteEnCoursName = 0;           //Indique le nom de la remote courante.
    public static int RemoteEnCoursID = -1;        //Indique l'ID de la remote courante.
    public CharSequence ConnexionIP = "";   //Indique l'IP de la connexion courante.
    public int PlayPause = 0;
    public static String message = "";
    public RemoteAudioVideo ClassRemoteAudio = new RemoteAudioVideo();
    private GestureDetector gestureScanner;
 
       
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accueil);
        gestureScanner = new GestureDetector(MenuAndroid.this, new OurGestureListener());
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event){
        if(gestureScanner.onTouchEvent(event))
            return true;
        else 
            return false;
    }
    
    //Méthode qui se déclenchera lorsque vous appuierez sur le bouton menu du téléphone
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
 
        //Création d'un MenuInflater qui va permettre d'instancier un Menu XML en un objet Menu
        MenuInflater inflater = getMenuInflater();
        //Instanciation du menu XML spécifier en un objet Menu
        inflater.inflate(R.layout.menu, menu);
 
        return true;
     }
    
       //Méthode qui se déclenchera au clic sur un item
    @Override
      public boolean onOptionsItemSelected(MenuItem item) {
         //On regarde quel item a été cliqué grâce à son id et on déclenche une action
         switch (item.getItemId()) {
            case R.id.option:
               Toast.makeText(MenuAndroid.this, "Option", Toast.LENGTH_SHORT).show();
               return true;
            case R.id.connexion:
                setContentView(R.layout.connexion);
                FixParametreVueConnexion();
                return true;
            case R.id.remote:
                setContentView(R.layout.remoteselection);
                //Toast.makeText(MenuAndroid.this, message, Toast.LENGTH_SHORT).show();
                FixParametreVueRemote();
                return true;
           case R.id.quitter:
               //Pour fermer l'application il suffit de faire finish()
               finish();
               return true;
         }
         return false;}
    
    
    //Permet de modifier la remote courante 
    public void RemoteEnCoursChange(CharSequence Selection, int ID){
        
        if("Audio/Video".equals(Selection)){
            RemoteEnCoursName = REMOTE_AUDIO;
        }
        else if("Présentation".equals(Selection)){
            RemoteEnCoursName = REMOTE_PRESENTATION;
        }
        else{
            RemoteEnCoursName = REMOTE_TRACKPAD;
        }
        
        RemoteEnCoursID = ID;
    }
    
    public void AfficheRemoteAudioVideo(){
        setContentView(R.layout.remoteaudio);
    }
    
    public void AfficheRemotePresentation(){
        setContentView(R.layout.remotepresentation);
    }
    
    public void AfficheRemoteTrackPad(){
        
        setContentView(R.layout.remotetrackpad);
    }
    
    public void AfficheNoRemote() {
        setContentView(R.layout.accueil);
    }

    //Prépare l'ouverture de la vue remoteselection
    private void FixParametreVueRemote() {
        
        if(RemoteEnCoursID !=  -1){
            RadioGroup RadioGroup = (RadioGroup)findViewById(R.id.radiogroup1);
            RadioGroup.check(RemoteEnCoursID);
        }
        
        //RemoteEnCoursName = 0;
        
    }
    
    public void DisplayAffichage(View v) {
        
        RadioGroup RadioGroup = (RadioGroup)findViewById(R.id.radiogroup1);
        RadioButton RadioSelect = (RadioButton)findViewById(RadioGroup.getCheckedRadioButtonId());
        RemoteEnCoursChange(RadioSelect.getText(), RadioGroup.getCheckedRadioButtonId()); 
            
            switch(RemoteEnCoursName){
                case NO_REMOTE:
                    AfficheNoRemote();
                break;
                case REMOTE_AUDIO: 
                    AfficheRemoteAudioVideo();
                break;
                case REMOTE_PRESENTATION: 
                    AfficheRemotePresentation();
                break;
                case REMOTE_TRACKPAD: 
                    AfficheRemoteTrackPad();
                break;
            }
            
        }
    
    public void DisplayAffichage() {
        
        switch(RemoteEnCoursName){
                case NO_REMOTE:
                    AfficheNoRemote();
                break;
                case REMOTE_AUDIO: 
                    AfficheRemoteAudioVideo();
                break;
                case REMOTE_PRESENTATION: 
                    AfficheRemotePresentation();
                break;
                case REMOTE_TRACKPAD: 
                    AfficheRemoteTrackPad();
                break;
            }
    }

    private void FixParametreVueConnexion() {
        
        if(ConnexionIP != ""){
            EditText text = (EditText)findViewById(R.id.edit1);
            text.setText(ConnexionIP);
            TextView erreur = (TextView)findViewById(R.id.txterreur);
            erreur.setText("");
            RemoteEnCoursName = 0;
        }
    }

    
    public void ValiderConnexion(View v) throws InterruptedException{
        EditText text = (EditText)findViewById(R.id.edit1);
        ConnexionIP = text.getText();
        
        
        if(!AdresseCorrect()){
            ConnexionIP = "";
            setContentView(R.layout.connexion);
            TextView erreur = (TextView)findViewById(R.id.txterreur);
            erreur.setText("Erreur : L'adresse IP est invalide !");  
        }
        else{
        
            //Ajouter ici le changement d'IP pour la connexion
            InfoServer.setIpServer(ConnexionIP.toString());
            
            if(CallRPC.ExecuteVLC(EActions.ETAT, true) == 0){
                DisplayAffichage();  
            }else{
                //setContentView(R.layout.connexion);
                TextView erreur = (TextView)findViewById(R.id.txterreur);
                erreur.setText("Erreur : La connexion n'a pas pu être établi !");
                InputMethodManager inm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                EditText monEdit = (EditText) findViewById(R.id.edit1);
                inm.hideSoftInputFromInputMethod(v.getWindowToken(), 0);
            }    
        
        }
        
    }
    
    public boolean AdresseCorrect(){
        if((ConnexionIP.toString()).matches(regex)){
            return true;
        }
        else{
            return false;
        }
    }


    public void Play(View v) {
        
        //Changement icone play en pause
        if(PlayPause == 0){
            ImageButton bouton = (ImageButton)findViewById(R.id.btnplay);
            bouton.setImageResource(R.drawable.buttonpause);
            PlayPause = 1;
        }
        else{
            ImageButton bouton = (ImageButton)findViewById(R.id.btnplay);
            bouton.setImageResource(R.drawable.buttonplay);
            PlayPause = 0;
        }
        
        try {
            CallRPC.ExecuteVLC(EActions.PLAY, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void Pause(View v) {

        //Changement icone pause en play
        ImageButton bouton = (ImageButton)findViewById(R.id.btnplay);
        bouton.setImageResource(R.drawable.buttonplay);
        try {
            CallRPC.ExecuteVLC(EActions.PAUSE, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Stop(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.STOP, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Next(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.NEXT, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Previous(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.PREVIOUS, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Ralentit(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.RALENTIT, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void AvanceRapide(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.AVANCERAPIDE, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Mute(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.MUTE, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void VolumeUp(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.VOLUMEUP, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void VolumeDown(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.VOLUMEDOWN, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Repeat(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.REPEAT, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Random(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.RANDOM, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void NextDiapo(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.NEXTDIAPO, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void PreviousDiapo(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.PREVIOUSDIAPO, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void StopDiapo(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.STOPDIAPO, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void StartDiapo(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.STARTDIAPO, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void WhiteOn(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.WHITEON, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void WhiteOff(View v) {
        try {
            CallRPC.ExecuteVLC(EActions.WHITEOFF, false);
        } catch (InterruptedException ex) {
            Logger.getLogger(MenuAndroid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void onGestureStarted(GestureOverlayView gov, MotionEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onGesture(GestureOverlayView gov, MotionEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onGestureEnded(GestureOverlayView gov, MotionEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onGestureCancelled(GestureOverlayView gov, MotionEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public int move(int deplacementX, int deplacementY) {
        CallRPC.ExecutePADMove(EActions.MOVE, deplacementX, deplacementY);
        return 0;
    }


        
}				

